#
# This file is part of WebCL.
# Project home page: http://webcl.nokiaresearch.com/
#

# This makefile relies on certain variables exported from parent makefile.
ifneq ($(MKFILE_MAGIC),bab7e365-4d06-4ebb-89bf-d5cd239fbad7)
$(error Build configuration not loaded! Run make from parent directory.)
endif

# Default compiler and linker. These settings can be overridden
# in buildoncig.inc .
ifeq ($(CXX),)
 CXX = CL.exe
endif
ifeq ($(LD),)
 LD = link.exe
endif


# Gecko
GECKO_DEFINES = -D XPCOM_GLUE -D XPCOM_GLUE_USE_NSPR -D JS_NO_JSVAL_JSID_STRUCT_TYPES
GECKO_INCLUDES = -I"$(MS_REAL_SDK_PATH)/include"
GECKO_CXXFLAGS =
GECKO_LDFLAGS = -LIBPATH:"$(MS_REAL_SDK_PATH)/lib" \
 xul.lib mozjs.lib xpcom.lib xpcomglue_s_nomozalloc.lib nspr4.lib


# WebCL
WEBCL_DEFINES = \
 -D WEBCL_VERSION_MAJOR=$(VERSION_MAJOR) -D WEBCL_VERSION_MINOR=$(VERSION_MINOR) \
 -D WEBCL_VERSION_RELEASE=$(VERSION_RELEASE) -D WEBCL_VERSION_REV=$(VERSION_REV)
WEBCL_INCLUDES = -I../public -I.
WEBCL_CXXFLAGS =
WEBCL_LDFLAGS =
ifeq ($(ENABLE_DEBUG), 1)
WEBCL_DEFINES += -D WEBCL_ENABLE_DEBUG
WEBCL_CXXFLAGS += -Zi
WEBCL_LDFLAGS += -DEBUG
endif
ifeq ($(ENABLE_LOG), 1)
WEBCL_DEFINES += -D WEBCL_ENABLE_LOG -D WEBCL_LOG_LEVEL_DEFAULT=$(LOG_LEVEL_DEFAULT)
endif
ifdef LOG_TO_FILE
WEBCL_DEFINES += -D WEBCL_LOG_TO_FILE=$(LOG_TO_FILE)
endif
ifeq ($(ENABLE_TRACE_FUNCTIONS), 1)
WEBCL_DEFINES += -D WEBCL_TRACE_FUNCTIONS
endif


DEFINES += -D WIN32 -D _WINDOWS -D _USRDLL -D XP_WIN -D XP_WIN32 -D _WINDLL \
 $(GECKO_DEFINES) $(WEBCL_DEFINES)
INCLUDES += $(GECKO_INCLUDES) $(WEBCL_INCLUDES)
CXXFLAGS += $(GECKO_CXXFLAGS) $(WEBCL_CXXFLAGS) \
 -nologo -WX- -O2 -Oy- -GL \
 -Gm- -EHsc -MT -GS -fp:precise \
 -Zc:forScope -Fo"../$(RELEASE)/" -Gd -TP
LDFLAGS += $(GECKO_LDFLAGS) $(WEBCL_LDFLAGS) \
 -nologo -MACHINE:X86 
#-incremental:no


BUILD_PREFIX = ../$(RELEASE)/
TARGET_NAME = $(COMPONENT_NAME)
TARGET = $(BUILD_PREFIX)$(TARGET_NAME).dll


# Default target
all: $(TARGET)


# Source files
SOURCES = $(wildcard *.cpp)
OBJECTS = $(SOURCES:%.cpp=$(BUILD_PREFIX)%.obj)


ifneq ($(BUILD_PREFIX),)
$(BUILD_PREFIX):
	@if ! test -z "$(BUILD_PREFIX)"; then mkdir -p $(BUILD_PREFIX) >/dev/null 2>&1 ; fi ; true
endif

# Compile WebCL object files
$(BUILD_PREFIX)%.obj: %.cpp $(BUILD_PREFIX)
	$(CXX) -c $(DEFINES) $(INCLUDES) $(CXXFLAGS) $<


# Link the library
$(TARGET): $(OBJECTS)
	$(LD) -OUT:"$(TARGET)" $(LDFLAGS) \
 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib \
 shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib \
 -SUBSYSTEM:WINDOWS -OPT:REF -OPT:ICF -LTCG -TLBID:1 \
 -IMPLIB:"$(BUILD_PREFIX)\$(TARGET_NAME).lib" \
 -DLL \
 $^


# Clean
clean:
	@rm -f "$(BUILD_PREFIX)"/*.obj 2>/dev/null ; true
	@rm -f "$(TARGET)" 2>/dev/null ; true
	@rm -f "$(BUILD_PREFIX)/$(TARGET_NAME).exp" 2>/dev/null ; true
	@rm -f "$(BUILD_PREFIX)/$(TARGET_NAME).lib" 2>/dev/null ; true


# In addition to clean: remove backup files and build dirs
ultra-clean: clean
	@rm -f ./*~ 2>/dev/null ; true
	@if ! test -z "$(BUILD_PREFIX)"; then rmdir "$(BUILD_PREFIX)" 2>/dev/null ; fi ; true


.PHONY: all clean ultra-clean ensureBuildDirExists
